package com.example.myapplicationwork.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

/**
 * SharedPreferences工具类
 * 用于管理Token、用户信息和自动登录状态
 */
public class SharedPreferencesUtil {
    private static final String TAG = "SharedPreferencesUtil";
    private static final String PREF_NAME = "user_prefs";
    private static final String KEY_TOKEN = "auth_token";
    private static final String KEY_AUTO_LOGIN = "auto_login";
    private static final String KEY_USER_EMAIL = "user_email";
    private static final String KEY_USER_ID = "user_id";
    private static final String KEY_USER_NAME = "user_name";

    private static SharedPreferencesUtil instance;
    private final SharedPreferences preferences;

    private SharedPreferencesUtil(Context context) {
        preferences = context.getApplicationContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    }

    /**
     * 静态初始化方法
     */
    public static void init(Context context) {
        if (instance == null) {
            synchronized (SharedPreferencesUtil.class) {
                if (instance == null) {
                    instance = new SharedPreferencesUtil(context.getApplicationContext());
                }
            }
        }
    }

    /**
     * 获取单例实例
     */
    public static synchronized SharedPreferencesUtil getInstance(Context context) {
        if (instance == null) {
            init(context.getApplicationContext());
        }
        return instance;
    }

    /**
     * 保存Token
     */
    public void saveToken(String token) {
        preferences.edit().putString(KEY_TOKEN, token).apply();
    }

    /**
     * 获取Token
     */
    public String getToken() {
        return preferences.getString(KEY_TOKEN, null);
    }

    /**
     * 清除Token
     */
    public void clearToken() {
        preferences.edit().remove(KEY_TOKEN).apply();
    }

    /**
     * 设置自动登录状态
     */
    public void setAutoLogin(boolean isAutoLogin) {
        preferences.edit().putBoolean(KEY_AUTO_LOGIN, isAutoLogin).apply();
    }

    /**
     * 获取自动登录状态
     */
    public boolean isAutoLogin() {
        return preferences.getBoolean(KEY_AUTO_LOGIN, false);
    }

    /**
     * 保存用户邮箱
     */
    public void saveUserEmail(String email) {
        preferences.edit().putString(KEY_USER_EMAIL, email).apply();
    }

    /**
     * 获取用户邮箱
     */
    public String getUserEmail() {
        return preferences.getString(KEY_USER_EMAIL, null);
    }

    /**
     * 保存用户ID
     */
    public void saveUserId(String userId) {
        preferences.edit().putString(KEY_USER_ID, userId).apply();
    }

    /**
     * 获取用户ID
     */
    public String getUserId() {
        return preferences.getString(KEY_USER_ID, null);
    }

    /**
     * 保存用户名
     */
    public void saveUserName(String userName) {
        preferences.edit().putString(KEY_USER_NAME, userName).apply();
    }

    /**
     * 获取用户名
     */
    public String getUserName() {
        return preferences.getString(KEY_USER_NAME, null);
    }

    /**
     * 保存用户登录信息
     */
    public void saveLoginInfo(String token, String email, String userId, String userName, boolean isAutoLogin) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(KEY_TOKEN, token);
        editor.putString(KEY_USER_EMAIL, email);
        editor.putString(KEY_USER_ID, userId);
        editor.putString(KEY_USER_NAME, userName);
        editor.putBoolean(KEY_AUTO_LOGIN, isAutoLogin);
        editor.apply();
        Log.d(TAG, "用户登录信息已保存");
    }

    /**
     * 清除所有用户数据
     * 包括Token、自动登录状态和用户信息
     */
    public void clearAllData() {
        preferences.edit()
                .remove(KEY_TOKEN)
                .remove(KEY_AUTO_LOGIN)
                .remove(KEY_USER_EMAIL)
                .remove(KEY_USER_ID)
                .remove(KEY_USER_NAME)
                .apply();
        Log.d(TAG, "所有用户数据已清除");
    }

    /**
     * 检查是否已登录
     */
    public boolean isLoggedIn() {
        return getToken() != null;
    }
}